//
//  UnNormalTableView.m
//  StationManager
//
//  Created by 张佳强 on 17/3/8.
//  Copyright © 2017年 Helong. All rights reserved.
//

#import "UnNormalTableView.h"
#import "DataCell.h"

@implementation UnNormalTableView

- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style {
    
    self = [super initWithFrame:frame style:style];
    if (self) {

        self.showsVerticalScrollIndicator = NO;
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.unNormalArray = [NSMutableArray arrayWithCapacity:0];
           }
    return self;
}

//解析本地json数据
- (NSDictionary *)jsonDict {
    
    NSString * filePath = [[NSBundle mainBundle] pathForResource:@"Maintenance" ofType:@"json"];
    NSData * jsonData = [[NSData alloc]initWithContentsOfFile:filePath];
    id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:nil];
    NSDictionary * dict = jsonObject[@"unNormal"];
    return dict;
}

//移除菜单
- (void)hide {
    
    [self.listTableView removeFromSuperview];
    self.listTableView = nil;
    [self.bgView removeFromSuperview];
    self.bgView = nil;
}

//更改不常用信息的数据源
- (void)changeUnormalDataSource:(NSNotification *)notify {
    
    NSDictionary * dict = (NSDictionary *)notify.object;
    NSInteger row = [dict[@"row"] integerValue];
    NSInteger section = [dict[@"section"] integerValue];
    NSMutableArray * array = [[NSMutableArray alloc]initWithCapacity:0];
    [array addObjectsFromArray:self.unNormalArray[section]];
    [array replaceObjectAtIndex:row withObject:dict[@"data"]];
    [self.unNormalArray replaceObjectAtIndex:section withObject:array];
    [self reloadData];
    
    [self.listTableView removeFromSuperview];
    self.listTableView = nil;
    [self.bgView removeFromSuperview];
    self.bgView = nil;
}

#pragma TableView - delegate

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeUnormalDataSource:) name:@"changeUnormalData" object:nil];
    
    UILabel * headText = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 35)];
    headText.font = [UIFont systemFontOfSize:15.0];
    headText.text = _textArray[section];
    return headText;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 35;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return 0.1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return self.unNormalArray.count;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   
    return [self.unNormalArray[section] count];
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString * iden = @"unNormal";
    DataCell * cell = [tableView cellForRowAtIndexPath:indexPath];
    if (cell == nil) {
        cell = [[DataCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:iden];
    }
    if (indexPath.row == 0) {
        cell.titleLabel.text = @"先前状态：";
    }
    if (indexPath.row == 1) {
        cell.titleLabel.text = @"运维措施：";
    }
    if (indexPath.row == 2) {
        cell.titleLabel.text = @"运维结果：";
    }
    cell.dataLabel.backgroundColor = [UIColor colorWithRed:239/255.0 green:239/255.0 blue:244/255.0 alpha:1.0];
    cell.spaceLabel.backgroundColor = [UIColor colorWithRed:239/255.0 green:239/255.0 blue:244/255.0 alpha:1.0];
    if (indexPath.row%2 == 0) {
        cell.dataLabel.frame = CGRectMake(90, 2, SCREEN_WIDTH-90, 36);
        cell.spaceLabel.frame = CGRectMake(80, 2, 10, 36);
    }else{
        cell.dataLabel.frame = CGRectMake(90, 0, SCREEN_WIDTH-90, 40);
        cell.spaceLabel.frame = CGRectMake(80, 0, 10, 40);
    }

    cell.dataLabel.text = self.unNormalArray[indexPath.section][indexPath.row];
    cell.iconImageView.image = [UIImage imageNamed:@"jiantou1"];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 40;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [self deselectRowAtIndexPath:indexPath animated:NO];
    self.bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT*3)];
    self.bgView.backgroundColor = [UIColor clearColor];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hide)];
    [self.bgView addGestureRecognizer:tap];
    [self addSubview:self.bgView];
    
    NSDictionary * dict = [self jsonDict];
    
    if ([self.stationType isEqualToString:@"T1"]) {
        _listArray =dict[@"list"][@"storm"];
    }
    else if ([self.stationType isEqualToString:@"T2"]) {
        _listArray =dict[@"list"][@"twoFactor"];
    }
    else  {
        _listArray =dict[@"list"][@"fourFactor"];
    }
    
    if (indexPath.section == _unNormalArray.count-1&&indexPath.row>0) {
        self.listTableView = [[ListTableView alloc]initWithFrame:CGRectMake(80, ([self rectForRowAtIndexPath:indexPath].origin.y)-([_listArray[indexPath.section][indexPath.row] count]*40), SCREEN_WIDTH-85, [_listArray[indexPath.section][indexPath.row] count]*40) style:UITableViewStylePlain];
        
    }else{
        self.listTableView = [[ListTableView alloc]initWithFrame:CGRectMake(80, ([self rectForRowAtIndexPath:indexPath].origin.y)+40, SCREEN_WIDTH-85, [_listArray[indexPath.section][indexPath.row] count]*40) style:UITableViewStylePlain];
        
    }
    self.listTableView.dataArray = _listArray[indexPath.section][indexPath.row];
    self.listTableView.sectionTag = indexPath.section;
    self.listTableView.rowTag = indexPath.row;
    self.listTableView.tableViewTag = 200;
    self.listTableView.vcName = @"Maintain";
    [self.listTableView reloadData];
    [self addSubview:self.listTableView];
}

#pragma ScrollView - delegate

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    [self reloadData];
}

@end
